<?php

class Bet_sb
{

    public $table_name;
    public $db;
    public $tbl;
    public $gametype;
    public $bet4subArr = array();

    public function Bet_sb($gametype = 1, $x = "", $cid = -1)
    {
        global $g_gametype_3d;
        global $g_bet4subArr;
        $this->tbl = $g_gametype_3d[$gametype]['en'];
        $this->table_name = $x . "bet_" . $g_gametype_3d[$gametype]['en'];
        if (0 - 1 < $cid)
        {
            $this->db = new DB_CEN($cid);
        }
        else
        {
            $this->db = new DB();
        }
        $this->gametype = $gametype;
        $this->bet4subArr = isset($g_bet4subArr[$gametype]) ? $g_bet4subArr[$gametype] : array();
    }

    public function getMyBetRS($id, $lotto_id)
    {
        $sql = "SELECT * FROM {$this->table_name}";
        $sql .= " WHERE user_id = {$id}";
        $sql .= " AND lotto_id = {$lotto_id}";
        $sql .= " ORDER BY createtime desc,id desc, bettype, numbers";
        return $this->db->query($sql);
    }

    public function getMyBetLimitRS($id)
    {
        if ($this->gametype == 0)
        {
            $sql = "SELECT a.*, b.account,c.round, c.open FROM bet a";
            $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
            $sql .= " LEFT JOIN lotto c ON (a.lotto_id = c.id)";
            $sql .= " WHERE a.id={$id} LIMIT 1";
        }
        else
        {
            $sql = "SELECT a.*, b.account,c.round, c.open FROM " . $this->table_name . " a";
            $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
            $sql .= " LEFT JOIN lotto_{$this->tbl} c ON (a.lotto_id = c.id)";
            $sql .= " WHERE a.id={$id} LIMIT 1";
        }
        return $this->db->query($sql);
    }

    public function getBigBetListRS($lotto_id, $checkVal, $time)
    {
        $rs = new RecordSet();
        $sql = "SELECT a.*, b.account, b.name  FROM {$this->table_name} a";
        $sql .= " LEFT JOIN k_user b ON (b.id=a.user_id)";
        $sql .= " WHERE";
        $sql .= " a.lotto_id = {$lotto_id}";
        $sql .= " AND a.createtime > {$time}";
        $sql .= " AND a.amount > {$checkVal}";
        $sql .= " ORDER BY a.createtime desc, a.id desc, a.bettype, a.numbers";
        $rs->open($sql, "0");
        return $rs;
    }
	//下注所产生的金额 已用额度
    public function getSumAmountBet($id, $lotto_id, $paperOpen = "")
    {
		return 0;
        $reVal = 0;
        $paperOpenArr = explode("-", $paperOpen);
        $paperInt = intval($paperOpenArr[2]);
        $sql = "!!SELECT amount_3d FROM k_user_bet";
        $sql .= " WHERE user_id = {$id}";
        $sql .= " AND lotto_id={$paperInt}";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getSumAmount($id, $lotto_id, $paperOpen = "")
    {
        $reVal = 0;
        $othrTbl = "";
        switch ($this->gametype)
        {
            case 1 :
                $othrTbl = "_3t";
                break;
            case 2 :
                $othrTbl = "_3d";
                break;
        }
        $sql = "!!SELECT sum(amount) as amount FROM {$this->table_name}";
        $sql .= " WHERE user_id = {$id}";
        $sql .= " AND lotto_id = {$lotto_id}";
        $sql .= " AND rame IN (0,9)";
        $sql .= " UNION ALL ";
        $sql .= " SELECT sum(a.amount) as amount FROM bet{$othrTbl} a";
        $sql .= " LEFT JOIN lotto{$othrTbl} b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open = '{$paperOpen}'";
        $sql .= " AND a.rame IN (0,9)";
        $result = $this->db->query($sql);

        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        return $reVal;
    }

    public function getSumWinMoney($id, $lotto_id, $paperOpen)
    {
        return 0;
    }

    public function getSumAmount3S($id, $open)
    {
        $reVal = 0;
        $sql = "!!SELECT sum(a.amount) FROM {$this->table_name} a";
        $sql .= " LEFT JOIN lotto_{$this->tbl} b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open = {$open}";
        $sql .= " AND a.rame IN (0,9)";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getSumAmountAllMem($ids, $lotto_id, $paperOpen = "")
    {
        $reVal = 0;
        $sql = "!!SELECT sum(amount) FROM " . $this->table_name;
        if ($ids === "")
        {
            $sql .= " WHERE 1=0 ";
        }
        else
        {
            $sql .= " WHERE user_id IN (" . $ids . ")";
            $sql .= " AND lotto_id = " . $lotto_id;
            $sql .= " AND rame IN (0,9)";
        }
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getBetHistoryResult($id, $lotto_id)
    {
        $sql = "SELECT sum(amount) as amount,sum(b1) as b1,sum(a1) as a1 FROM " . $this->table_name;
        $sql .= " WHERE user_id = " . $id;
        $sql .= " AND lotto_id = " . $lotto_id;
        $result = $this->db->query($sql);
        return mysql_fetch_array($result, MYSQL_ASSOC);
    }

    public function getLastBet($id, $lotto_id, $end = 12)
    {
        $reArr = array();
        $sql = "SELECT * FROM " . $this->table_name;
        $sql .= " WHERE user_id = {$id}";
        $sql .= " AND lotto_id = {$lotto_id}";
        $sql .= " AND rame IN (0,9)";
        $sql .= " ORDER BY createtime DESC, id DESC, bettype, numbers";
        $sql .= " LIMIT 0," . $end;
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            $reArr[] = $row;
        }
        if (0 < count($reArr))
        {
            return $reArr;
        }
        return false;
    }

    public function getSumNumberAmount($id, $lotto_id, $bettype, $number, $subid = 0)
    {
        if (is_array($number))
        {
            $numberStr = implode(",", $number);
        }
        else
        {
            $numberStr = $number;
        }
        $reVal = 0;
        $sql = "!!SELECT amount FROM bet_amounts_" . $this->tbl;
        $sql .= " WHERE user_id = " . $id;
        $sql .= " AND bettype = " . $bettype;
        $sql .= " AND subid = " . $subid;
        $sql .= " AND numbers = '{$numberStr}'";
        $sql .= " AND lotto_id = " . $lotto_id;
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getSumNumberAmountBet($id, $lotto_id, $bettype, $number, $subid = 0)
    {
        if (is_array($number))
        {
            $numberStr = implode(",", $number);
        }
        else
        {
            $numberStr = $number;
        }
        $reVal = 0;
        $sql = "!!SELECT sum(amount) FROM " . $this->table_name;
        $sql .= " WHERE user_id = " . $id;
        $sql .= " AND bettype = " . $bettype;
        $sql .= " AND subid = " . $subid;
        $sql .= " AND numbers = '{$numberStr}'";
        $sql .= " AND lotto_id = " . $lotto_id;
        $sql .= " AND rame = 0";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getSumBettypeAmount($id, $lotto_id, $bettype, $subid = 0)
    {
        $reVal = 0;
        $sql = "!!SELECT sum(amount) FROM " . $this->table_name;
        $sql .= " WHERE user_id = " . $id;
        $sql .= " AND bettype = " . $bettype;
        $sql .= " AND subid = " . $subid;
        $sql .= " AND lotto_id = " . $lotto_id;
        $sql .= " AND rame = 0";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getMemBetList($bettypeStr, $num, $page, $ids, $timeStart, $timeEnd, $lid = "", $where = "", $idsWhere = true)
    {
        $bettypeArr = explode("_", $bettypeStr);
        $bettype = $bettypeArr[0];
        if (isset($bettypeArr[1]))
        {
            $subid = $bettypeArr[1];
        }
        else
        {
            $subid = 0;
        }
        $rs = new RecordSet();
        $pagesize = 20;
        $sql = "SELECT a.*, b.account,c.round, c.open FROM {$this->table_name} a";
        $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
        $sql .= " LEFT JOIN lotto_{$this->tbl} c ON (a.lotto_id = c.id)";
        $sql .= " WHERE a.bettype={$bettype} and a.subid={$subid} ";
        $num = trim($num);
        if ($bettype == 46)
        {
            if ($num < 6)
            {
                $sql .= " AND a.numbers LIKE '{$num},%'";
            }
            else
            {
                if ($num < 12)
                {
                    $sql .= " AND a.numbers LIKE '%,{$num},%'";
                }
                else
                {
                    $sql .= " AND a.numbers LIKE '%,{$num}'";
                }
            }
        }
        else
        {
            $sql .= " AND a.numbers='" . trim($num) . "'";
        }
        if ($lid == "")
        {
            $sql .= " AND c.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "'";
        }
        else
        {
            $sql .= " AND a.lotto_id ={$lid} ";
        }
        if ($ids === "")
        {
            $sql .= " AND 1 = 0 {$where}";
        }
        else
        {
            if ($idsWhere)
            {
                $sql .= " AND a.user_id IN ({$ids}) ";
            }
            $sql .= " " . $where;
        }
        $sql .= " ORDER BY c.open DESC, a.createtime DESC, a.id DESC";
        $rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getMemBetGList($bettypeStr, $num, $page, $ids, $timeStart, $timeEnd, $lid = "", $where = "", $idsWhere = true)
    {
        $bettypeArr = explode("_", $bettypeStr);
        $bettype = $bettypeArr[0];
        if (isset($bettypeArr[1]))
        {
            $subid = $bettypeArr[1];
        }
        else
        {
            $subid = 0;
        }
        $rs = new RecordSet();
        $pagesize = 20;
        $sql = "SELECT a.*, b.account,c.round, c.open FROM " . $this->table_name . " a";
        $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
        $sql .= " LEFT JOIN lotto_{$this->tbl} c ON (a.lotto_id = c.id)";
        $sql .= " WHERE a.bettype={$bettype} and a.subid={$subid}";
        $sql .= " AND a.numbers LIKE '%" . $num . "%'";
        if ($lid == "")
        {
            $sql .= " AND c.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "'";
        }
        else
        {
            $sql .= " AND a.lotto_id = {$lid}";
        }
        if ($ids === "")
        {
            $sql .= " AND 1 = 0 {$where}";
        }
        else
        {
            if ($idsWhere)
            {
                $sql .= " AND a.user_id IN (" . $ids . ") ";
            }
            $sql .= " " . $where;
        }
        $sql .= " ORDER BY c.open DESC, a.createtime DESC, a.id DESC";
        $rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getMemBetRList($bettypeStr, $numArr, $page, $ids, $timeStart, $timeEnd, $lid = "", $where = "", $idsWhere = true)
    {
        $bettypeArr = explode("_", $bettypeStr);
        $bettype = $bettypeArr[0];
        if (isset($bettypeArr[1]))
        {
            $subid = $bettypeArr[1];
        }
        else
        {
            $subid = 0;
        }
        $rs = new RecordSet();
        $pagesize = 20;
        $sql = "SELECT a.*, b.account,c.round, c.open FROM " . $this->table_name . " a";
        $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
        $sql .= " LEFT JOIN lotto_{$this->tbl} c ON (a.lotto_id = c.id)";
        $sql .= " WHERE bettype={$bettype} and subid = {$subid}";
        $sql .= " AND a.numbers LIKE '%" . $numArr[0] . "%,%" . $numArr[1] . "%,%" . $numArr[2] . "%'";
        if ($lid == "")
        {
            $sql .= " AND c.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "'";
        }
        else
        {
            $sql .= " AND a.lotto_id = {$lid} ";
        }
        if ($ids === "")
        {
            $sql .= " AND 1 = 0 {$where}";
        }
        else
        {
            if ($idsWhere)
            {
                $sql .= " AND a.user_id IN ({$ids}) ";
            }
            $sql .= " " . $where;
        }
        $sql .= " ORDER BY c.open DESC, a.createtime DESC, a.id DESC";
        $rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getMemBetAllList($bettypeStr, $page, $ids, $timeStart, $timeEnd, $lid = "", $where = "", $idsWhere = true)
    {
        $rs = new RecordSet();
        $bettypeArr = explode("_", $bettypeStr);
        $bettype = $bettypeArr[0];
        if (isset($bettypeArr[1]))
        {
            $subid = $bettypeArr[1];
        }
        else
        {
            $subid = 0;
        }
        $pagesize = 20;
        $sql = "SELECT a.*, b.account,c.round, c.open FROM {$this->table_name} a";
        $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
        $sql .= " LEFT JOIN lotto_{$this->tbl} c ON (a.lotto_id = c.id)";
        $sql .= " WHERE";
        if ($bettypeStr !== "")
        {
            $sql .= " bettype=" . $bettype . " AND subid = {$subid} AND ";
        }
        if ($lid == "")
        {
            $sql .= " c.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "'";
        }
        else
        {
            $sql .= " a.lotto_id = {$lid}";
        }
        if ($ids === "")
        {
            $sql .= " AND 1 = 0 {$where}";
        }
        else
        {
            if ($idsWhere)
            {
                $sql .= " AND user_id IN ({$ids}) ";
            }
            $sql .= " " . $where;
        }
        $sql .= " ORDER BY a.createtime DESC, a.id DESC";
        $rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getUserSumNumberAmount($idstr, $bettypeStr, $number, $timeStart, $timeEnd, $lid = "", $where = "", $idsWhere = true)
    {
        $bettypeArr = explode("_", $bettypeStr);
        $bettype = $bettypeArr[0];
        if (isset($bettypeArr[1]))
        {
            $subid = $bettypeArr[1];
        }
        else
        {
            $subid = 0;
        }
        $reVal = false;
        $sqlLike = "";
        $gArr = array(24, 25, 26, 27, 28, 30, 31, 32, 33, 34);
        if ($bettype == "35")
        {
            $number1 = substr($number, 0, 1);
            $number2 = substr($number, 1, 1);
            $number3 = substr($number, 2, 1);
            $numberT = "%{$number1}%,%{$number2}%,%{$number3}%";
            $sqlLike = " AND numbers LIKE '{$numberT}'";
        }
        else if ($bettype == "46")
        {
            if ($number < 6)
            {
                $sqlLike = " AND numbers LIKE '{$number},%'";
            }
            else
            {
                if ($number < 12)
                {
                    $sqlLike = " AND numbers LIKE '%,{$number},%'";
                }
                else
                {
                    $sqlLike = " AND numbers LIKE '%,{$number}'";
                }
            }
        }
        else if (in_array($bettype, $gArr))
        {
            $sqlLike = " AND numbers LIKE '%{$number}%'";
        }
        else
        {
            $sqlLike = " AND numbers = '{$number}'";
        }
        if ($number == "" || $number == "all")
        {
            $sqlLike = "";
        }
        $sql = "SELECT sum(a.amount) as amount, sum(a.b1) as b1, sum(a.a1) as a1  FROM " . $this->table_name;
        $sql .= " a LEFT JOIN lotto_{$this->tbl} b ON (a.lotto_id = b.id) ";
        if ($idstr === "")
        {
            $sql .= " WHERE 1 = 0 ";
        }
        else
        {
            $sql .= " WHERE ";
            if ($idsWhere)
            {
                $sql .= " a.user_id IN ({$idstr})";
            }
            else
            {
                $sql .= " 1 ";
            }
        }
        if ($bettypeStr !== "")
        {
            $sql .= " AND bettype = {$bettype} AND subid = {$subid} ";
        }
        $sql .= $sqlLike;
        if ($lid == "")
        {
            $sql .= " AND ( b.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "')";
        }
        else
        {
            $sql .= " AND ( a.lotto_id = {$lid})";
        }
        if ($where == "")
        {
            $sql .= " AND a.rame IN (0,9)";
        }
        else
        {
            $sql .= " " . $where;
        }
        $result = $this->db->query($sql);
        if ($result)
        {
            return mysql_fetch_array($result, MYSQL_ASSOC);
        }
        return $reVal;
    }

    public function getDataListTotal($lotto_id, $bettype, $acl, $showids, $split = 0, $diskStr = "", $speStr = "", $speStrAll = false)
    {
        if ($acl == 300)
        {
            $acl = 255;
        }
        $reArr = array();
        $sqlAddDisk = "";
        $tempArr = array("a" => "1", "b" => "2", "c" => "3", "d" => "4");
        $bettypeArr = explode("_", $bettype);
        if (count($bettypeArr) < 2)
        {
            $subid = 0;
        }
        else
        {
            $subid = $bettypeArr[1];
        }
        $bettype = $bettypeArr[0];
        if ($diskStr != "")
        {
            $sqlAddDisk = " AND disk = '" . $tempArr[$diskStr] . "'";
        }
        if ($acl < 255)
        {
            $highPriStr = "";
        }
        else
        {
            $highPriStr = "HIGH_PRIORITY";
        }
        $sql = "SELECT {$highPriStr} count(*) as totalnum, ";
        if ($split == 1)
        {
            $sql .= " sum(amount*percent{$acl} / 100) as amount";
            $sql .= " ,sum(amount * rate * percent{$acl} /100) as amount_rate";
            if ($acl != 255)
            {
                $sAcl = $acl - 1;
                $sql .= " ,sum(amount * (back{$sAcl} - back{$acl}) *percent{$acl} /10000 ) as back2";
            }
            else
            {
                $sql .= " , 0 as back2";
            }
            $sql .= " ,sum(amount * (100-back5) * percent{$acl} /10000) as back1";
        }
        else
        {
            $sql .= " sum(amount) as amount";
            $sql .= " ,sum(amount * rate) as amount_rate";
            if ($acl != 255)
            {
                $sAcl = $acl - 1;
                $sql .= " ,sum(amount * (back{$sAcl} - back{$acl}) /100 ) as back2";
            }
            else
            {
                $sql .= " , 0 as back2";
            }
            $sql .= " ,sum(amount * (100-back5) /100) as back1";
        }
        $sql .= " , MAX(createtime) as updatetime";
        $sql .= " FROM {$this->table_name}";
        if ($speStrAll)
        {
            $sql .= " WHERE lotto_id = '{$lotto_id}'";
        }
        else
        {
            if ($speStr == "")
            {
                $sql .= " WHERE bettype = {$bettype} AND subid = {$subid}";
            }
            else
            {
                $sql .= " WHERE bettype IN ({$speStr})";
            }
            $sql .= " AND lotto_id = '{$lotto_id}'";
        }
        $sql .= $sqlAddDisk;
        $sql .= " AND rame IN(0,9)";
        if ($showids === "")
        {
            $sql .= " AND 1 = 0 ";
        }
        else if ($acl < 255)
        {
            $sql .= " AND user_id IN({$showids})";
        }
        $result = $this->db->query($sql);
        if ($row = mysql_fetch_array($result, MYSQL_ASSOC))
        {
            return $row;
        }
        return false;
    }

    public function dbInsert($fields)
    {
        $sql = "INSERT INTO {$this->table_name} (";
        $sql .= implode(",", array_keys($fields));
        $sql .= ") VALUES (";
        $sql .= implode(",", array_values($fields));
        $sql .= ")";

        if (!$this->db->query($sql))
        {
            return false;
        }
		$result=mysql_insert_id($this->db->handle_master);
		//扣除额度
		$this->db->query('update k_user set balance=balance-'.$fields['amount'].' where id='.$fields['user_id']);
        return $result;
    }

    public function getMyMemberIdByAcl($id, $acl)
    {
        $reVal = "";
        switch ($acl)
        {
            case "255" :
                $sql = "SELECT id FROM k_user WHERE acl = '1'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $reVal = implode(",", $idArr);
                break;
            case "5" :
                $sql = "SELECT id FROM k_user WHERE acl = '4' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id4str = implode(",", $idArr);
                if ($id4str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '3' AND prev_id IN(" . $id4str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id3str = implode(",", $idArr);
                if ($id3str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '2' AND prev_id IN (" . $id3str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id2str = implode(",", $idArr);
                if ($id2str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id IN (" . $id2str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
            case "4" :
                $sql = "SELECT id FROM k_user WHERE acl = '3' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id3str = implode(",", $idArr);
                if ($id3str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '2' AND prev_id IN (" . $id3str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id2str = implode(",", $idArr);
                if ($id2str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id IN (" . $id2str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
            case "3" :
                $sql = "SELECT id FROM k_user WHERE acl = '2' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id2str = implode(",", $idArr);
                if ($id2str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id IN (" . $id2str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
            case "2" :
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
        }
        return $reVal;
    }

}

class Bet_3s extends Bet_sb
{

    public function getSumAmountBet($id, $lotto_id, $paperOpen)
    {
        $reVal = 0;
        $paperOpenArr = explode("-", $paperOpen);
        $paperInt = $paperOpenArr[2];
        $sql = "!!SELECT amount_3s FROM k_user_bet";
        $sql .= " WHERE user_id = {$id}";
        $sql .= " AND lotto_id='{$paperInt}'";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getSumAmount($id, $lotto_id, $paperOpen)
    {
        $reVal = 0;
        $sql = "!!SELECT sum(a.amount) FROM bet_3s1 a";
        $sql .= " LEFT JOIN lotto_3s1 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_3s2 a";
        $sql .= " LEFT JOIN lotto_3s2 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_3s3 a";
        $sql .= " LEFT JOIN lotto_3s3 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_3s4 a";
        $sql .= " LEFT JOIN lotto_3s4 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_3s5 a";
        $sql .= " LEFT JOIN lotto_3s5 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_3s6 a";
        $sql .= " LEFT JOIN lotto_3s6 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_5h a";
        $sql .= " LEFT JOIN lotto_5h b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_8h a";
        $sql .= " LEFT JOIN lotto_8h b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_8h2 a";
        $sql .= " LEFT JOIN lotto_8h2 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_8h3 a";
        $sql .= " LEFT JOIN lotto_8h3 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(a.amount) FROM bet_pk10 a";
        $sql .= " LEFT JOIN lotto_pk10 b ON (a.lotto_id=b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND a.rame IN (0,9)";
        $sql .= " AND b.open='{$paperOpen}'";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        return $reVal;
    }

    public function getSumAmountAllMem($ids, $lotto_id, $paperOpen)
    {
        $reVal = 0;
        if ($ids === "")
        {
            $sql = "!!SELECT sum(a.amount) FROM {$this->table_name} a\r\n             LEFT JOIN lotto_{$this->tbl} b ON (a.lotto_id=b.id)\r\n             WHERE 1 = 0 ";
        }
        else
        {
            $sql = "!!SELECT sum(a.amount) FROM {$this->table_name} a\r\n             LEFT JOIN lotto_{$this->tbl} b ON (a.lotto_id=b.id)\r\n             WHERE a.user_id IN ({$ids})\r\n             AND b.open='{$paperOpen}'                  \r\n             AND a.rame IN (0,9)";
        }
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

    public function getSumWinMoneyBet($id, $lotto_id, $paperOpen)
    {
        global $conf;
        $reVal = 0;
        $paperOpenArr = explode("-", $paperOpen);
        $paperInt = intval($paperOpenArr[2]);
        $floatBack3s = intval($conf->get("floatBack_3s"));
        if ($floatBack3s)
        {
            $floatBack3sStr = "+ a1";
        }
        else
        {
            $floatBack3sStr = "";
        }
        $sql = "!!SELECT (b1 {$floatBack3sStr}) as amount02 FROM k_user_bet ";
        $sql .= " WHERE user_id = {$id}";
        $sql .= " AND lotto_id={$paperInt}";
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return intval($reVal);
    }

    public function getSumWinMoney($id, $lotto_id, $paperOpen)
    {
        global $conf;
        $reVal = 0;
        $floatBack3s = intval($conf->get("floatBack_3s"));
        if ($floatBack3s)
        {
            $floatBack3sStr = "+ a.a1";
        }
        else
        {
            $floatBack3sStr = "";
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_3s1 a ";
        $sql .= " LEFT JOIN lotto_3s1 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_3s2 a ";
        $sql .= " LEFT JOIN lotto_3s2 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_3s3 a ";
        $sql .= " LEFT JOIN lotto_3s3 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_3s4 a ";
        $sql .= " LEFT JOIN lotto_3s4 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_3s5 a ";
        $sql .= " LEFT JOIN lotto_3s5 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_3s6 a ";
        $sql .= " LEFT JOIN lotto_3s6 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_5h a ";
        $sql .= " LEFT JOIN lotto_5h b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_8h a ";
        $sql .= " LEFT JOIN lotto_8h b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_8h2 a ";
        $sql .= " LEFT JOIN lotto_8h2 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_8h3 a ";
        $sql .= " LEFT JOIN lotto_8h3 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        $sql = "!!SELECT sum(IF(a.b1 - a.a1 > 0,a.b1 - a.a1 + a.amount, 0 ) {$floatBack3sStr}) as amount02 FROM bet_pk10 a ";
        $sql .= " LEFT JOIN lotto_pk10 b ON (a.lotto_id = b.id)";
        $sql .= " WHERE a.user_id = {$id}";
        $sql .= " AND b.open='{$paperOpen}'";
        $sql .= " AND a.rame = 9";
        $result = $this->db->query($sql);
        while ($row = mysql_fetch_array($result))
        {
            $reVal += $row[0];
        }
        return intval($reVal);
    }
	//获取存款取款记录 浮动额度
    public function getAmountFloat($id, $gtype = 3)
    {
        return 0;
		$reVal = 0;
        $sql = "!!SELECT IFNULL(SUM(amount),0) as amount FROM amounts_float WHERE gtype = {$gtype} AND user_id = {$id}";
		
        $result = $this->db->query($sql);
        if ($result)
        {
            $row = mysql_fetch_array($result);
            $reVal = $row[0];
        }
        return $reVal;
    }

}

?>